"""
追踪和监控相关模型
"""
from tortoise import fields
from .base import BaseModel


class TrackingDevice(BaseModel):
    """追踪设备模型"""
    
    device_id = fields.CharField(max_length=100, unique=True, description="设备ID")
    device_type = fields.CharField(
        max_length=20,
        default="gps",
        description="设备类型: gps-GPS定位, rfid-RFID标签, beacon-蓝牙信标, sensor-传感器"
    )
    brand = fields.CharField(max_length=100, null=True, description="品牌")
    model = fields.CharField(max_length=100, null=True, description="型号")
    
    # 状态信息
    status = fields.CharField(
        max_length=20,
        default="active",
        description="状态: active-活跃, inactive-非活跃, offline-离线, damaged-损坏"
    )
    battery_level = fields.IntField(null=True, description="电池电量(%)")
    signal_strength = fields.IntField(null=True, description="信号强度")
    
    # 配置信息
    update_interval = fields.IntField(default=60, description="更新间隔(秒)")
    
    # 关联信息
    cargo = fields.ForeignKeyField("models.Cargo", related_name="tracking_devices", null=True, description="关联货物")
    equipment = fields.ForeignKeyField("models.Equipment", related_name="tracking_devices", null=True, description="关联装备")
    
    class Meta:
        table = "tracking_devices"
        table_description = "追踪设备表"


class LocationHistory(BaseModel):
    """位置历史记录模型"""
    
    # 设备信息
    device = fields.ForeignKeyField("models.TrackingDevice", related_name="location_history", description="追踪设备")
    
    # 位置信息
    latitude = fields.DecimalField(max_digits=10, decimal_places=7, description="纬度")
    longitude = fields.DecimalField(max_digits=10, decimal_places=7, description="经度")
    altitude = fields.DecimalField(max_digits=8, decimal_places=2, null=True, description="海拔(m)")
    accuracy = fields.DecimalField(max_digits=6, decimal_places=2, null=True, description="精度(m)")
    
    # 运动信息
    speed = fields.DecimalField(max_digits=6, decimal_places=2, null=True, description="速度(km/h)")
    direction = fields.DecimalField(max_digits=6, decimal_places=2, null=True, description="方向(度)")
    
    # 地址信息
    address = fields.CharField(max_length=500, null=True, description="地址")
    country = fields.CharField(max_length=100, null=True, description="国家")
    province = fields.CharField(max_length=100, null=True, description="省份")
    city = fields.CharField(max_length=100, null=True, description="城市")
    
    # 时间信息
    recorded_at = fields.DatetimeField(description="记录时间")
    
    class Meta:
        table = "location_history"
        table_description = "位置历史记录表"
        indexes = [
            ["device", "recorded_at"],
            ["latitude", "longitude"],
        ]


class GeofenceArea(BaseModel):
    """地理围栏区域模型"""
    
    name = fields.CharField(max_length=200, description="围栏名称")
    description = fields.TextField(null=True, description="围栏描述")
    
    # 围栏类型和形状
    fence_type = fields.CharField(
        max_length=20,
        default="circle",
        description="围栏类型: circle-圆形, polygon-多边形, rectangle-矩形"
    )
    
    # 圆形围栏参数
    center_latitude = fields.DecimalField(max_digits=10, decimal_places=7, null=True, description="中心纬度")
    center_longitude = fields.DecimalField(max_digits=10, decimal_places=7, null=True, description="中心经度")
    radius = fields.DecimalField(max_digits=8, decimal_places=2, null=True, description="半径(m)")
    
    # 多边形围栏参数
    polygon_points = fields.JSONField(null=True, description="多边形顶点坐标")
    
    # 围栏规则
    rule_type = fields.CharField(
        max_length=20,
        default="enter",
        description="规则类型: enter-进入, exit-离开, both-进入和离开"
    )
    
    # 状态信息
    is_enabled = fields.BooleanField(default=True, description="是否启用")
    
    class Meta:
        table = "geofence_areas"
        table_description = "地理围栏区域表"


class GeofenceEvent(BaseModel):
    """地理围栏事件模型"""
    
    device = fields.ForeignKeyField("models.TrackingDevice", related_name="geofence_events", description="追踪设备")
    geofence = fields.ForeignKeyField("models.GeofenceArea", related_name="events", description="地理围栏")
    
    event_type = fields.CharField(
        max_length=20,
        description="事件类型: enter-进入, exit-离开"
    )
    
    # 位置信息
    latitude = fields.DecimalField(max_digits=10, decimal_places=7, description="纬度")
    longitude = fields.DecimalField(max_digits=10, decimal_places=7, description="经度")
    
    # 时间信息
    occurred_at = fields.DatetimeField(description="发生时间")
    
    # 处理状态
    is_processed = fields.BooleanField(default=False, description="是否已处理")
    processed_at = fields.DatetimeField(null=True, description="处理时间")
    
    class Meta:
        table = "geofence_events"
        table_description = "地理围栏事件表"
